package com.study.scala

import scala.beans.{BeanProperty, BooleanBeanProperty}

/**
 * @program: spark2.3.2
 * @author: Zhoujian
 * @date: 2022-06-11 19:21
 * @version: 1.0
 * @description:  函数 和 方法的使用
 * */
object App {
	def main(args: Array[String]): Unit = {
		val add1 : (Int,Int) => Int = { (x,y) =>x+y }

		// 方法 : 类中的函数
		def aa(x:Int,y:Int): Int = {
			x + y
		}
		// scala> aa: (x: Int, y: Int)Int
		// 函数 : 为完成某一功能的程序语句的集合
		def add3 : (Int,Int) => Int  =  {(x:Int,y:Int) => x+y}
		// scala> add3: (Int, Int) => Int
		val add7 : (Int,Int) => Int  =  (x:Int,y:Int) => x+y
		// scala> add7: (Int, Int) => Int = <function2>

		// 至简写法
		val add8  : (Int,Int) => Int = (x,y) => x + y
		// scala> add8: (Int, Int) => Int
		val add9  =  (x:Int,y:Int) => x+y
		// scala> def add9  =  (x:Int,y:Int) => x+y

		// 高阶函数  此方法中的函数可以称为匿名函数
		def add5(i: Int, j: Int = 5, f: (Int, Int) => Int) = {
			f(i, j)
		}
		// scala> add5: (i: Int, j: Int, f: (Int, Int) => Int)Int
		// 高阶函数  柯里化
		def add6(i: Int, j: Int = 5)( f: (Int, Int) => Int) = {
			f(i, j)
		}
		// scala> add6: (i: Int, j: Int)(f: (Int, Int) => Int)Int


		val f = aa _
		// scala> f: (Int, Int) => Int = <function2>

		/** 可认为下面两个传参一直*/
		def m1(x: => Int) = List(x,x+1)
		// scala> m1: (x: => Int)List[Int]
		def m2(x:Int) = List(x,x*2)
		// scala> m2: (x: Int)List[Int]


		val add = (x : Int) => x + 100
		def add2 = (x : Int) => {
			x + 100
		}


		val i = 1
		val j = 2

		def a(f: Int => Int) = f(i)
		def b(f: (Int,Int) => Int) = f(3,j)

		println(a(add2))
		println(b(add3))
		println(b((_,x) => x))

		// 数组测试
		val arr = Array(1,2,3,4,5,6,7)
		println(arr(1))


	}

}


class App(name: String) {
	//声明辅助构造方法，方法名为this
	def this() {
		this("name")
	}
	// scala 注解，可以生成get set 方法，用于
	@BeanProperty
	var status = ""
	// Boolean 类型可用该注解生成isFlag方法
	@BooleanBeanProperty
	var flag = true
	println("主构造")
}
